/*
 * app_shell.c
 *
 *  Created on: Jun 23, 2010
 *      Author: jmonk
 */
#include "mox.h"
#include "mox_module.h"
#include "mox_threads.h"
#include "mox_compat.h"
#include <xccompat.h>
#include <xs1_g4000b-512.h>
#include <print.h>

void app_start_shell(void)
{
	chanend mox;
	register int tmp;
	chanend in, out;
	char umod[8] = "UARTMOD";
	unsigned thd_id;
	unsigned info[] = { TH_TYPE_APP, 1};
	mox_msg thd_info = {
			TH_PARAM,
			2*sizeof(unsigned),
			(unsigned char *)info
	};
	mox_msg *msg = NULL;

	THREAD_START_GET_CHAN(mox);

	thd_id = thd_std_start(mox, thd_info);

	do {
		if (msg && msg->content) m_free(mox, msg->content);
		m_free(mox, msg);
		msg = thd_comm_msg_recv(mox);
	} while (msg->type != TH_PARAM);

	if (msg->content) m_free(mox, msg->content);
	msg->type = TH_START_MOD;
	msg->content = umod;
	msg->size = 2 * sizeof(unsigned);

	thd_comm_msg_send(mox, *msg);
	do {
		if (msg->content) m_free(mox, msg->content);
		m_free(mox, msg);
		msg = thd_comm_msg_recv(mox);
	} while (msg->type != TH_PARAM);
	if (((unsigned *)msg->content)[0] == 1) {
		tmp = ((unsigned *)msg->content)[1];
	} else {
		printstrln("Strange Thread Number: About to Crash");
	}
//	tmp = 7;
	do {
		if (msg->content) m_free(mox, msg->content);
		m_free(mox, msg);
		msg = thd_comm_msg_recv(mox);
	} while (msg->type != TH_PARAM);

	printstrln("Application Started Uart Module");

	msg->type = TH_CON_REQ;
	msg->content = (unsigned char *)m_malloc(mox, 2 * sizeof(unsigned));
	if (msg->content) {
		((unsigned *)msg->content)[0] = tmp;
		((unsigned *)msg->content)[1] = 0;
		msg->size = 2 * sizeof(unsigned);
		thd_comm_msg_send(mox, *msg);

		do {
			if (msg->content) m_free(mox, msg->content);
			m_free(mox, msg);
			msg = thd_comm_msg_recv(mox);
		} while (msg->type != TH_PARAM);
		in = ((unsigned *)msg->content)[0];
	} else {
		printstrln("Unable to connect to UARTMOD: About to Crash");
	}

	printstrln("Application connected to Uart Module");

	msg->type = TH_CON_REQ;
	msg->content = (unsigned char *)m_malloc(mox, 2 * sizeof(unsigned));
	if (msg->content) {
		((unsigned *)msg->content)[0] = tmp;
		((unsigned *)msg->content)[1] = 1;
		msg->size = 2 * sizeof(unsigned);
		thd_comm_msg_send(mox, *msg);

		do {
			if (msg->content) m_free(mox, msg->content);
			m_free(mox, msg);
			msg = thd_comm_msg_recv(mox);
		} while (msg->type != TH_PARAM);
		in = ((unsigned *)msg->content)[0];
	} else {
		printstrln("Unable to connect to UARTMOD: About to Crash");
	}

	if (!in || !out) {
		printstrln("Unable to Connect to UARTMOD");
		while (1);
	}

	printstrln("Application connected to Uart Module");

	while (1) {
		for (tmp = 0; tmp < 5; tmp++) {
			mox_chan_out_wd(in, 'A');
		}
		tmp = mox_chan_in_noblock(out);
		if (tmp != -1) {
			mox_chan_out_wd(in, tmp);
		}

		tmp = mox_chan_in_wd(mox);
		if (tmp != -1) {
			if (tmp == MOX_MSG) {
				mox_chan_out_wd(mox, TH_NACK);
				if (msg->content) m_free(mox, msg->content);
				m_free(mox, msg);
				msg = thd_comm_msg_recv(mox);
			} else {
				mox_chan_out_wd(mox, TH_ACK);
			}
		}

	}

}
